package cn.school.servlet;

import cn.school.entity.Admin;
import cn.school.service.AdminService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.net.URLEncoder;

@WebServlet( "/loginServlet")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String name=request.getParameter("userName");///request.getParameterValues("userName")[0];
        String pass=request.getParameter("userPass");
        Admin login = AdminService.INST.login(name, pass);
        if(login!=null){
             //用户的信息放入session
            HttpSession session = request.getSession();//获得会活对象
            session.setAttribute("USER_LOGIN",login);
            //把用户名存入cookie
            Cookie c=new Cookie("loginname",name);
            c.setMaxAge(3600*24*7);//活多久
            response.addCookie(c);//@!!!!!
            response.sendRedirect("main.jsp");
            return;
        }

        request.setAttribute("msg","错误的用户名或者密码！");
        request.getRequestDispatcher("/login.jsp").forward(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
